home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / Thread Manager Extension 2.0.1 / Sample Applications / Power Examples / SortPicts / Source / Events.cp next >
Encoding:
Text File  |  1994-06-03  |  12.1 KB  |  486 lines  |  [TEXT/MPS ]

  1. /*************************************************************************************
  2.  *
  3.  *    Object Oriented Shell
  4.  *
  5.  *    Events.cp    -    C Source
  6.  *
  7.  *      Copyright © Apple Computer, Inc. 1988-1993
  8.  *      All rights reserved.
  9.  *
  10.  *    This file contains the interface between the User and the GWorldObj objects.
  11.  *    This file will call the GWorldObj class which inherits from WindowObj.
  12.  *
  13.  *************************************************************************************/
  14.  
  15. #include "main.h"
  16. #include <Traps.h>
  17. #include "Windows.h"
  18. #include "WindowObj.h"
  19. #include "WindowObj.Link"
  20.  
  21.  
  22. /**************************************************************************************
  23.  
  24.     MainEventLoop
  25.     
  26.     Get events forever and handles them by calling Handle Event.  First, call
  27.     AdjustCursor to set our cursor shape and to set the cursor region.  We
  28.     then call WaitNextEvent() to get the event.  This is OK, because we know
  29.     we're running on System 6.0 or later by this time. If we got an event, we
  30.     handle it by calling Handle Event().  But before doing that, we call
  31.     AdjustCursor again in case our application had falled asleep under MultiFinder.
  32.     
  33. ***************************************************************************************/
  34. void    MainEventLoop()
  35. {
  36.     RgnHandle    cursorRgn, copyCursorRgn;
  37.     Boolean        gotEvent;
  38.     EventRecord    event;
  39. //    Point        mouse;
  40. //    WindowPtr    theWindow;
  41.     long        sleepTime;
  42. //    Point        mouseLoc;
  43.     
  44.     cursorRgn = NewRgn();
  45.     while( !gQuit)
  46.     {
  47.         AppAdjustCursor( cursorRgn);
  48.         if( gInBackground == false)
  49.         {
  50.             copyCursorRgn = EmptyRgn( cursorRgn) ? (RgnHandle)0 : cursorRgn;
  51.             sleepTime = copyCursorRgn ? GetCaretTime() : 1000;
  52.         }
  53.         else
  54.         {
  55.             sleepTime = 1000;
  56.             copyCursorRgn = (RgnHandle)nil;
  57.         }
  58.         sleepTime = 1;
  59.         gotEvent = WaitNextEvent( everyEvent, &event, sleepTime, copyCursorRgn);
  60.         AppAdjustCursor( (RgnHandle)nil);
  61.         if( gotEvent)
  62.         {
  63.             HandleEvent( &event);
  64.         }
  65.         else
  66.             HandleNoEvent( );
  67.     }
  68.     DisposeRgn( cursorRgn);
  69. }
  70.  
  71. /**************************************************************************************
  72.  
  73.     HandleEvent
  74.     
  75.     Do the right thing for an event.  Determine what kind of event it is and
  76.     call the appropraite routines.
  77.     
  78. ***************************************************************************************/
  79. void    HandleEvent( EventRecord *event)
  80. {
  81.     switch( event->what)
  82.     {
  83.         case mouseDown:
  84.                 HandleMouseDown( event);
  85.                 break;
  86.         case keyDown:
  87.         case autoKey:
  88.                 HandleKeyPress( event);
  89.                 break;
  90.         case activateEvt:
  91.                 HandleActivate( event);
  92.                 break;
  93.         case updateEvt:
  94.                 HandleUpdate( event);
  95.                 break;
  96.         case diskEvt:
  97.                 HandleDiskInsert( event);
  98.                 break;
  99.         case osEvt:
  100.                 HandleOSEvent( event);
  101.                 break;
  102.         default:
  103.                 HandleNoEvent( );
  104.                 break;
  105.     }
  106. }
  107.  
  108. /**************************************************************************************
  109.  
  110.     HandleActivate
  111.     
  112.     This is called when a window is activated or deactivated.  In this sample,
  113.     the Window Manager's handling of activate and deactivate events is
  114.     sufficeint.  Other applications may have TextEdit records, controls, lists,
  115.     etc., to activate/deactivate.
  116.     
  117. ***************************************************************************************/
  118. void    HandleActivate(EventRecord *event)
  119. {
  120.     WindowPtr    theWindow;
  121.     Boolean        becomingActive;
  122.     
  123.     theWindow = (WindowPtr) event->message;
  124.     becomingActive = (event->modifiers & activeFlag) != 0;
  125.     if( IsAppWindow(theWindow) )
  126.     {
  127.         if( IsAppWindow(theWindow))
  128.         {
  129.             ToWindowObj(theWindow)->DrawGrowIconObj( );
  130.             ToWindowObj(theWindow)->ActivateObj( becomingActive);
  131.         /* ActivateWindow(theWindow, becomingActive) */ ;
  132.         }
  133.         else
  134.         {
  135.             DrawGrowIcon( theWindow);
  136.             if( becomingActive == true)
  137.             {
  138.                 SelectWindow( theWindow);
  139.             }
  140.         }
  141.     }
  142. }
  143.  
  144. /**************************************************************************************
  145.  
  146.     HandleDiskInsert
  147.     
  148.     Called when we get a disk-inserted event.  Check the upper word of the
  149.     event message; if it's nonzero, then bad disk was inserted, and it needs to be formateed. 
  150.     
  151. ***************************************************************************************/
  152. void    HandleDiskInsert(EventRecord *event)
  153. {
  154.     WindowPtr    theWindow;
  155.     Point        aPoint = {100, 100};
  156.     
  157.     theWindow = FrontWindow();
  158.     if( IsAppWindow(theWindow) == true)
  159.         ToWindowObj(theWindow)->DiskInsertObj( event);
  160.     else
  161.         if( HiWrd(event->message) != noErr)
  162.         {
  163.             (void) DIBadMount(aPoint, event->message);
  164.         }
  165.         
  166. }
  167.  
  168. /**************************************************************************************
  169.  
  170.     HandleKeyPress
  171.     
  172.     The user pressed a key.  What are you going to do about it?
  173.     
  174. ***************************************************************************************/
  175. void    HandleKeyPress(EventRecord *event)
  176. {
  177.     char         key;
  178.     WindowPtr    theWindow;
  179.     long        menuAndItem;
  180.     WINDOWOBJ  *a;
  181.     
  182.     key = (char) ((event->message) & charCodeMask);
  183.     if( event->modifiers & cmdKey)
  184.     {
  185.         theWindow = FrontWindow();
  186.         if( IsAppWindow(theWindow))
  187.         {
  188.             ToWindowObj(theWindow)->AdjustMenusObj();
  189.             menuAndItem = MenuKey(key);
  190.             ToWindowObj(theWindow)->MenuObj( (short)(menuAndItem >> 16),
  191.                                              (short)(menuAndItem & 0xFFFF), event);
  192.         }
  193.         else
  194.         {
  195.             a = new WINDOWOBJ;
  196.             if( a)
  197.             {
  198.                 a->AdjustMenusObj();
  199.                 menuAndItem = MenuKey(key);
  200.                 a->MenuObj( (short)(menuAndItem >> 16),
  201.                             (short)(menuAndItem & 0xFFFF), event);
  202.                 delete a;
  203.             }
  204.             else
  205.             {
  206.                 menuAndItem = MenuKey(key);
  207.                 AppMenu( (short)(menuAndItem >> 16), (short)(menuAndItem & 0xFFFF));
  208.             }
  209.         }
  210.     }
  211.     else
  212.     {
  213.         theWindow = FrontWindow();
  214.         if( IsAppWindow(theWindow))
  215.         {
  216.             ToWindowObj(theWindow)->KeyPressObj( event);
  217.         }
  218.         else
  219.         {
  220.             a = new WINDOWOBJ;
  221.             if( a)
  222.             {
  223.                 a->AdjustMenusObj();
  224.                 a->KeyPressObj( event);
  225.                 delete a;
  226.             }
  227.         }
  228.         /* KeyPress(event) */;
  229.     }
  230. }
  231.  
  232. /**************************************************************************************
  233.  
  234.     HandleMouseDown
  235.     
  236.     Called to handle mouse clicks.  The user could have clicked anywhere, so
  237.     let's first find out where by calling FindWindow.  That returns a number
  238.     indicating where in the screen the mouse was clicked.  "switch" on that
  239.     number and call the appropriate routine.
  240.     
  241. ***************************************************************************************/
  242. void    HandleMouseDown(EventRecord *event)
  243. {
  244.     long        newSize;
  245.     Rect        growRect;
  246.     WindowPtr    theWindow;
  247.     short        part;
  248.     GrafPtr        oldPort;
  249.     long        menuAndItem;
  250.     
  251.     part = FindWindow(event->where, &theWindow);
  252.     
  253.     switch( part)
  254.     {
  255.         case inMenuBar:
  256.             theWindow = FrontWindow();
  257.             if( IsAppWindow(theWindow))
  258.             {
  259.                 ToWindowObj(theWindow)->AdjustMenusObj();
  260.                 menuAndItem = MenuSelect(event->where);
  261.                 ToWindowObj(theWindow)->MenuObj((short)(menuAndItem >> 16), 
  262.                                                 (short)(menuAndItem & 0xFFFF),
  263.                                                  event);
  264.             }
  265.             else
  266.             {
  267.                 WINDOWOBJ           *a;
  268.                 a = new WINDOWOBJ;
  269.                 if( a)
  270.                 {
  271.                     a->AdjustMenusObj();
  272.                     menuAndItem = MenuSelect(event->where);
  273.                     a->MenuObj((short)(menuAndItem >> 16), 
  274.                                (short)(menuAndItem & 0xFFFF),
  275.                                 event);
  276.                     delete a;
  277.                 }
  278.             }
  279.             break;
  280.         case inSysWindow:
  281.             SystemClick(event, theWindow);
  282.             break;
  283.         case inContent:
  284.             if( theWindow != FrontWindow() )
  285.             {
  286.                 SelectWindow(theWindow);
  287.                 if( IsAppWindow(theWindow))
  288.                 {
  289. //                    if( ToWindowObj(theWindow)->useActivateClicks)
  290. //                    {
  291. //                        ToWindowObj(theWindow)->useActivateClicks = false;
  292. //                        HandleMouseDown( event);
  293. //                        ToWindowObj(theWindow)->useActivateClicks = true;
  294. //                    }
  295.                 }    
  296.             }
  297.             else
  298.             {
  299.                 if( IsAppWindow(theWindow))
  300.                 {
  301.                     GetPort( &oldPort);
  302.                     SetPort( theWindow);
  303.                     GlobalToLocal( &event->where);
  304.                     ToWindowObj(theWindow)->MouseClickObj( event);
  305.                     SetPort( oldPort);
  306.                 }
  307.                 /* ContentClick(event, theWindow */;
  308.             }
  309.             break;
  310.         case inDrag:
  311.             if( IsAppWindow(theWindow))
  312.                 ToWindowObj(theWindow)->DragWindowObj( event);
  313.             break;
  314.         case inGrow:
  315.             if( IsAppWindow(theWindow))
  316.                 ToWindowObj( theWindow)->GrowWindowObj( event);
  317.             else
  318.             {
  319.                 growRect = qd.screenBits.bounds;
  320.                 growRect.top = growRect.left = 80;        /* Arbitrary minimum size */
  321.                 newSize = GrowWindow(theWindow, event->where, &growRect);
  322.                 if( newSize != 0)
  323.                 {
  324.                     InvalidateScrollbars(theWindow);
  325.                     SizeWindow(theWindow, LoWrd(newSize), HiWrd(newSize), true);
  326.                     InvalidateScrollbars(theWindow);
  327.                 }
  328.             }
  329.             break;
  330.         case inGoAway:
  331.             if( TrackGoAway(theWindow, event->where))
  332.             {
  333.                 CloseAnyWindow(theWindow);
  334.             }
  335.             break;
  336.         case inZoomIn:
  337.         case inZoomOut:
  338.             if( TrackBox(theWindow, event->where, part))
  339.             {
  340.                 SetPort(theWindow);
  341.                 EraseRect( &theWindow->portRect);
  342.                 ZoomWindow(theWindow, part, true);
  343.                 InvalRect( &theWindow->portRect);
  344.             }
  345.             break;
  346.     }
  347. }
  348.  
  349. /**************************************************************************************
  350.  
  351.     HandleOSEvent
  352.     
  353.     Deal with OSEvents (formerly, app4Events).  These are messages that
  354.     Multi-Finder-- known as the Process Manager under System 7.0 -- sends to
  355.     us.  Here, we deal with the suspend and resume message.
  356.         
  357. ***************************************************************************************/
  358. void    HandleOSEvent(EventRecord *event)
  359. {
  360.     WindowPtr    theWindow;
  361.     switch ((event->message >> 24) & 0x00FF)
  362.     {
  363.         case suspendResumeMessage:
  364.         
  365.             /*    In our SIZE resource, we say that we are Multi-Finder aware.
  366.                 This means that we take on the responsibility of activating
  367.                 and deactivating our own windows on suspend/resume events. */
  368.             
  369.             gInBackground = (event->message & resumeFlag) == 0;
  370.             theWindow = FrontWindow();
  371.             if(theWindow != (WindowPtr)NIL)
  372.             {
  373.                 if( IsAppWindow(theWindow) == true)
  374.                 {
  375.                     ToWindowObj(theWindow)->DrawGrowIconObj();
  376.                     ToWindowObj(theWindow)->ActivateObj( !gInBackground);
  377.                 }
  378.             }
  379.             if( event->message & convertClipboardFlag )
  380.             {
  381.                 if( theWindow != (WindowPtr)NIL)
  382.                     ToWindowObj(theWindow)->ConvertScrapObj();
  383.                 else
  384.                     AppConvertScrap();
  385.             }
  386.             break;
  387.         case mouseMovedMessage:
  388.             break;
  389.     }
  390. }
  391.  
  392. /**************************************************************************************
  393.  
  394.     HandleUpdate
  395.  
  396.     This is called when an update event is received for a window.  It calls
  397.     UpdateWindowto draw the contents of an application window.  As an
  398.     efficeinncy measure that does not have to be followed, it calls the draing 
  399.     routine only if the visRgn is nonempty.  This will handle situation where
  400.     calculations for drawing or drawing itself is very time consuming.
  401.          
  402. ***************************************************************************************/
  403. void    HandleUpdate(EventRecord *event)
  404. {
  405.     WindowPtr    theWindow = (WindowPtr)event->message;
  406.     GrafPtr        oldPort;
  407.     
  408.     if( IsAppWindow(theWindow) )
  409.     {
  410.         GetPort( &oldPort);
  411.         SetPort( theWindow);
  412.         BeginUpdate( theWindow);
  413.         if( !EmptyRgn(theWindow->visRgn))
  414.         {
  415. //            SetPort(theWindow);
  416.             if( IsAppWindow(theWindow))
  417.             {
  418.                 ToWindowObj(theWindow)->UpdateObj( );
  419.                 ToWindowObj(theWindow)->DrawGrowIconObj( );
  420.             }
  421.         }
  422.         EndUpdate(theWindow);
  423.         SetPort( oldPort);
  424.     }
  425. }
  426.  
  427.  
  428.  
  429. /**************************************************************************************
  430.  
  431.     AppAdjustCursor
  432.  
  433.     The cursor needs to be adjusted.  The shell's default adjustment is to set
  434.     the cursor to an arrow.  The windows that you define need to set the cursor
  435.     to their own type of cursors.
  436.          
  437. ***************************************************************************************/
  438. void    AppAdjustCursor( RgnHandle cursorRgn)
  439. {
  440.     Point        mouseLoc;
  441.     WindowPtr    theWindow;
  442.     GrafPtr        oldPort;
  443.     
  444.     GetMouse( &mouseLoc);    // These are local coords!!
  445.     theWindow = FrontWindow();
  446.     if( IsAppWindow(theWindow) == true)
  447.     {    /* This calls the windows adjustCursor routine */
  448.         GetPort( &oldPort);
  449.         SetPort( theWindow);
  450.         GetMouse( &mouseLoc);    // These are local coords!!
  451.         
  452.         ToWindowObj(theWindow)->AdjustCursorObj
  453.                         (mouseLoc, cursorRgn);
  454.         SetPort( oldPort);
  455.     }
  456.     else
  457.     {    /* This is the application's AdjustCursorObj */
  458.         if( cursorRgn != (RgnHandle)NIL)
  459.         {
  460.             SetEmptyRgn( cursorRgn);        
  461.             SetCursor( &qd.arrow);
  462.         }
  463.     }
  464. }
  465.  
  466.  
  467.  
  468. /**************************************************************************************
  469.  
  470.     HandleNoEvent
  471.  
  472.     This will call the various idle handler's within the program.
  473.          
  474. ***************************************************************************************/
  475. void    HandleNoEvent( void)
  476. {
  477.     WindowPtr    theWindow;
  478.     
  479.     theWindow = FrontWindow();
  480.     if( IsAppWindow(theWindow) == true)
  481.     {
  482.         ToWindowObj(theWindow)->IdleObj();
  483.     }
  484. }
  485.  
  486.